package com.yb.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.baomidou.mybatisplus.generator.keywords.PostgreSqlKeyWordsHandler;

import java.util.Collections;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/17 23:25
 * FileName: MybatisplusGen
 * Description:
 */
public class MybatisplusPostgresqlGen {

    private static final String db="test";
    private static final String url="jdbc:postgresql://postgres:15433/postgres";
    private static final String username="postgres";
    private static final String password="postgres";

    private static final String packagePath="com.yb.guigu.test";
    private static final String outputDir="E://mybatisplus/src/main/java";

    private static final String mapperxmlOutputDir="E://mybatisplus/src/main/resources/mappers";

    public static void main(String[] args) {

        FastAutoGenerator.create( new DataSourceConfig.Builder(url, username, password).dbQuery(new PostgreSqlQuery()).typeConvert(new PostgreSqlTypeConvert())
                        .keyWordsHandler(new PostgreSqlKeyWordsHandler()))
                .globalConfig(builder -> {
                    builder.author("姚兵") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outputDir); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(packagePath) // 设置父包名
//                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperxmlOutputDir)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                            .disableSerialVersionUID()
                            .enableChainModel()
                            .enableLombok()
                            .enableRemoveIsPrefix()

                            .enableTableFieldAnnotation();
                    builder.controllerBuilder()
                                    .enableRestStyle();
                    builder.likeTable(new LikeTable(""));
//                    builder.addInclude("oauth2_authorization,oauth2_authorization_consent,oauth2_registered_client,sys_permission,sys_role,sys_role_permission,sys_user,sys_user_role".split(",")) ;// 设置需要生成的表名
//                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
